package services.common;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import models.common.bean.AnnualBean;
import models.common.bean.DealPlatBean;
import models.common.bean.DealPlatform;
import models.common.bean.EarningsBean;
import models.common.bean.MonthlySummary;
import models.common.entity.t_deal_platform;
import models.common.entity.t_deal_platform.DealOption;
import models.common.entity.t_deal_platform.DealType;

import org.apache.commons.lang.StringUtils;

import services.base.BaseService;

import common.constants.Constants;
import common.utils.DateUtil;
import common.utils.Factory;
import common.utils.PageBean;

import daos.common.DealPlatformDao;

/**
 * 平台交易记录的service的具体实现
 *
 * @description 
 *
 * @author DaiZhengmiao
 * @createDate 2015年12月22日
 */
public class DealPlatformService extends BaseService<t_deal_platform> {

	protected DealPlatformDao dealPlatformDao = null;
	protected static SettingService settingService = Factory.getService(SettingService.class);
	protected DealPlatformService() {
		this.dealPlatformDao = Factory.getDao(DealPlatformDao.class);
		
		super.basedao = this.dealPlatformDao;
	}

	/**
	 * 添加平台交易记录
	 *
	 * @param userId 用户id/可以使企业也可以是个人
	 * @param amount 金额
	 * @param dealRemark 平台收支类型备注(包括收支类型，操作类型和描述信息)
	 * @param dealRemarkParam dealRemark对应的参数 转换成对应的参数
	 * @return
	 *
	 * @author liaijun
	 * @createDate 2017/04/21
	 */
	public boolean addPlatformDeal(long userId, double amount, t_deal_platform.DealRemark dealRemark, Map<String, Object> dealRemarkParam,long bid_id,long invest_id,String company_name) {
		t_deal_platform deal = new t_deal_platform();
		deal.time = new Date();
		deal.user_id = userId;
		deal.amount = amount;
		deal.setRemark(dealRemark, dealRemarkParam);
		deal.bid_id = bid_id;
		deal.invest_id = invest_id;
		deal.company_name = company_name;
		return dealPlatformDao.save(deal);
	}

	/**
	 * 根据操作类型分页查找所有的平台交易记录(根据用户名关键字进行模糊查询)
	 *
	 * @param currPage 当前第几页
	 * @param pageSize 每页显示的数据量
	 * @param dealOption 操作类型(当操作类型为null时表示查询所有的)
	 * @param dealType 收支类型
	 * @param userName 用户名关键字
	 * @param exports 1:导出  default：不导出
	 * @return
	 *
	 * @author DaiZhengmiao
	 * @createDate 2015年12月23日
	 */
	public PageBean<DealPlatform> pageOfDealsByOption(int orderValue,int currPage, int pageSize, t_deal_platform.DealOption dealOption, t_deal_platform.DealType dealType,String userName,int exports) {
		
		StringBuffer countSQL = new StringBuffer("SELECT COUNT(1) FROM t_deal_platform tdp LEFT JOIN t_user tu ON tdp.user_id = tu.id WHERE 1=1");
		StringBuffer querySQL = new StringBuffer("SELECT tdp.id AS id,  tdp.time AS time ,tdp.operation AS operation,  tdp.user_id AS user_id,  tdp.amount AS amount,  tdp.type AS type, tdp.remark AS remark,IFNULL(tu.company_full_name,tu.name )AS name FROM t_deal_platform tdp LEFT JOIN t_user tu ON tdp.user_id = tu.id WHERE 1=1");
		
		Map<String, Object> condition = new HashMap<String, Object>();
		if(dealOption != null){
			countSQL.append(" AND operation=:operation ");
			querySQL.append(" AND operation=:operation ");
			condition.put("operation", dealOption.code);
		}
		if(dealType != null){
			countSQL.append(" AND type=:dealType ");
			querySQL.append(" AND type=:dealType ");
			condition.put("dealType", dealType.code);
		}
		if(StringUtils.isNotBlank(userName)){
			countSQL.append(" AND (tu.name LIKE :userName OR tu.company_full_name LIKE :userName ) ");
			querySQL.append(" AND (tu.name LIKE :userName OR tu.company_full_name LIKE :userName ) ");
			condition.put("userName", "%"+userName+"%");
		}
		querySQL.append(" ORDER BY id ");
		if(orderValue == 0){
			querySQL.append(" DESC");
			
		}
		if(exports == Constants.EXPORT){
			PageBean<DealPlatform> page = new PageBean<DealPlatform>();
			page.page = dealPlatformDao.findListBeanBySQL(querySQL.toString(), DealPlatform.class, condition);
			return page;
		}
		
		return dealPlatformDao.pageOfBeanBySQL(currPage, pageSize, countSQL.toString(), querySQL.toString(), DealPlatform.class,condition);
		
	}

	/**
	 * 根据交易类型 查询交易 收支总额(根据用户名关键字进行模糊查询)
	 *
	 * @param dealOption 1：平台奖励 2：闪电充值 3：充值手续费  4：提现手续费  5：本息垫付 6：逾期罚息  7：线下收款 8:投资服务费  9:融通服务费   10:转让服务费 
	 * @param dealType 1.收入  2.支出
	 * @param userName 用户名关键字
	 * @return
	 *
	 * @author liudong
	 * @createDate 2016年1月19日
	 */
	public double findDealPlatformTotalAmt(DealOption dealOption,DealType dealType,String userName,String projectName) {
		double totalAmt=0.00;
		
		//查询收支合计  总计=收入-支出
		if(dealType == null){
			//收入总额 
			double incomeAmt = dealPlatformDao.findDealPlatformTotalAmt(dealOption, DealType.INCOME,userName,projectName);
	
			//支出总额
			double expensesAmt = dealPlatformDao.findDealPlatformTotalAmt(dealOption, DealType.EXPENSES,userName,projectName);
			
			totalAmt = incomeAmt - expensesAmt;
		}
		else if(DealType.INCOME.equals(dealType)){
			totalAmt = dealPlatformDao.findDealPlatformTotalAmt(dealOption, dealType,userName,projectName);
		}
		else if(DealType.EXPENSES.equals(dealType)){
			totalAmt = dealPlatformDao.findDealPlatformTotalAmt(dealOption, dealType,userName,projectName)*(-1);
		}
		
		return totalAmt;
	}

	/**
	 * 查询平台收益报表
	 * @param currenPage 当前页
	 * @param pageSize 每页大小
	 * @param flag 个人和企业的标识 false个人true企业
	 * @param name 个人实名企业全称
	 * @param type 类型 收入和支出
	 * @param operation 操作类型
	 * @param startAmount 按条件筛选 起始金额
	 * @param endAmount按条件筛选 结束金额
	 * @return
	 */
	public PageBean<DealPlatBean> queryDealPlat(int currPage,int pageSize,int flag,String name,int type,int operation,double startAmount ,double endAmount,int exports){
		return dealPlatformDao.queryDealPlat(currPage, pageSize, flag, name, type, operation, startAmount, endAmount,exports);
	}
	/**
	 * 根据操作类型分页查找所有的平台交易记录(根据用户名关键字进行模糊查询)
	 *
	 * @param currPage 当前第几页
	 * @param pageSize 每页显示的数据量
	 * @param dealOption 操作类型(当操作类型为null时表示查询所有的)
	 * @param dealType 收支类型
	 * @param userName 用户名关键字
	 * @param exports 1:导出  default：不导出
	 * @return
	 *
	 * @author DaiZhengmiao
	 * @createDate 2015年12月23日
	 */
	public PageBean<EarningsBean> pageOfDealsByBidFee(int orderValue,int currPage, int pageSize, t_deal_platform.DealOption dealOption, t_deal_platform.DealType dealType,String userName,int exports) {
		return dealPlatformDao.pageOfDealsByBidFee(orderValue,currPage, pageSize, dealOption, dealType, userName, exports);
	}
	/**
	 * 根据操作类型分页查找所有的平台交易记录(根据用户名关键字进行模糊查询)
	 *
	 * @param currPage 当前第几页
	 * @param pageSize 每页显示的数据量
	 * @param dealOption 操作类型(当操作类型为null时表示查询所有的)
	 * @param dealType 收支类型
	 * @param userName 用户名关键字
	 * @param exports 1:导出  default：不导出
	 * @return
	 *
	 * @author DaiZhengmiao
	 * @createDate 2015年12月23日
	 */
	public PageBean<EarningsBean> pageOfDealsByInvestFee(int currPage, int pageSize, t_deal_platform.DealOption dealOption, t_deal_platform.DealType dealType,String userName,int exports) {
		return dealPlatformDao.pageOfDealsByInvestFee(currPage, pageSize, dealOption, dealType, userName, exports);
	}
	/**
	 * 平台统计月报表
	 *
	 * @param currPage 当前第几页
	 * @param pageSize 每页显示的数据量
	 * @param dealOption 操作类型(当操作类型为null时表示查询所有的)
	 * @param dealType 收支类型
	 * @param userName 用户名关键字
	 * @param exports 1:导出  default：不导出
	 * @return
	 *
	 * @author DaiZhengmiao
	 * @createDate 2015年12月23日
	 */
	public PageBean<MonthlySummary> pageOfMonthlyStatement(int currPage, int pageSize, String userName,int exports) {
		return dealPlatformDao.pageOfMonthlyStatement(currPage, pageSize,userName, exports);
	}
	/**
	 * 查找年度汇总数据
	 *
	 * @param currPage 当前第几页
	 * @param pageSize 每页显示的数据量
	 * @param dealOption 操作类型(当操作类型为null时表示查询所有的)
	 * @param dealType 收支类型
	 * @param userName 用户名关键字
	 * @param exports 1:导出  default：不导出
	 * @return
	 *
	 * @author DaiZhengmiao
	 * @createDate 2015年12月23日
	 */
	public PageBean<MonthlySummary> pageOfAnnual(int currPage, int pageSize,int exports,int year) {
		return dealPlatformDao.pageOfAnnual(currPage, pageSize, exports,year+"");
	}

	/**
	 *	根据时间查询
	 * @param orderValue
	 * @param currPage
	 * @param pageSize
	 * @param dealOption
	 * @param dealType
	 * @param time
	 * @param exports
	 * @return
	 * @author xiaojun
	 *@version 2018-4-14 
	 */
	public PageBean<DealPlatform> pageOfDealsByOption(int orderValue,
			int currPage, int pageSize, DealOption dealOption, DealType dealType,
			Date time, int exports) {

		StringBuffer countSQL = new StringBuffer("SELECT COUNT(1) FROM t_deal_platform tdp LEFT JOIN t_user tu ON tdp.user_id = tu.id WHERE 1=1");
		StringBuffer querySQL = new StringBuffer("SELECT tdp.id AS id,  tdp.time AS time ,tdp.operation AS operation,  tdp.user_id AS user_id,  tdp.amount AS amount,  tdp.type AS type, tdp.remark AS remark,IFNULL(tu.company_full_name,tu.name )AS name FROM t_deal_platform tdp LEFT JOIN t_user tu ON tdp.user_id = tu.id WHERE 1=1");
		
		Map<String, Object> condition = new HashMap<String, Object>();
		if(dealOption != null){
			countSQL.append(" AND operation=:operation ");
			querySQL.append(" AND operation=:operation ");
			condition.put("operation", dealOption.code);
		}
		if(dealType != null){
			countSQL.append(" AND type=:dealType ");
			querySQL.append(" AND type=:dealType ");
			condition.put("dealType", dealType.code);
		}
		
		//按提现时间昵称模糊查询
		if(time != null){
			String startTime = DateUtil.dateToString(DateUtil.todayFirstTime(time), "yyyy-MM-dd HH:mm:SS");
			String endTime = DateUtil.dateToString(DateUtil.todayLastTime(time), "yyyy-MM-dd HH:mm:SS");
			
			countSQL.append(" AND (tdp.time >=:startTime AND tdp.time <= :endTime)");
			querySQL.append(" AND (tdp.time >=:startTime AND tdp.time <= :endTime)");
			condition.put("startTime", startTime);
			condition.put("endTime", endTime);
		}
		querySQL.append(" ORDER BY id ");
		if(orderValue == 0){
			querySQL.append(" DESC");
			
		}
		if(exports == Constants.EXPORT){
			PageBean<DealPlatform> page = new PageBean<DealPlatform>();
			page.page = dealPlatformDao.findListBeanBySQL(querySQL.toString(), DealPlatform.class, condition);
			return page;
		}
		
		return dealPlatformDao.pageOfBeanBySQL(currPage, pageSize, countSQL.toString(), querySQL.toString(), DealPlatform.class,condition);
		
	}

	/**
	 * 根据时间查询
	 * @param enum1
	 * @param enum2
	 * @param userName
	 * @param time
	 * @return
	 * @author xiaojun
	 *@version 2018-4-14 
	 */
	public double findDealPlatformTotalAmt(DealOption dealOption, DealType dealType,
			String userName, Date time) {
		double totalAmt=0.00;
		
		//查询收支合计  总计=收入-支出
		if(dealType == null){
			//收入总额 
			double incomeAmt = dealPlatformDao.findDealPlatformTotalAmt(dealOption, DealType.INCOME,userName,time);
	
			//支出总额
			double expensesAmt = dealPlatformDao.findDealPlatformTotalAmt(dealOption, DealType.EXPENSES,userName,time);
			
			totalAmt = incomeAmt - expensesAmt;
		}
		else if(DealType.INCOME.equals(dealType)){
			totalAmt = dealPlatformDao.findDealPlatformTotalAmt(dealOption, dealType,userName,time);
		}
		else if(DealType.EXPENSES.equals(dealType)){
			totalAmt = dealPlatformDao.findDealPlatformTotalAmt(dealOption, dealType,userName,time)*(-1);
		}
		
		return totalAmt;
	}

	/**
	 *
	 * @param orderValue
	 * @param currPage
	 * @param pageSize
	 * @param enum1
	 * @param dealType
	 * @param time
	 * @param exports
	 * @return
	 * @author xiaojun
	 *@version 2018-4-14 
	 */
	public PageBean<EarningsBean> pageOfDealsByBidFee(int orderValue,
			int currPage, int pageSize, DealOption dealOption, DealType dealType,
			Date time, int exports) {
		return dealPlatformDao.pageOfDealsByBidFee(orderValue,currPage, pageSize, dealOption, dealType, time, exports);
	}

	/**
	 *
	 * @param currPage
	 * @param pageSize
	 * @param enum1
	 * @param dealType
	 * @param time
	 * @param exports
	 * @return
	 * @author xiaojun
	 *@version 2018-4-14 
	 */
	public PageBean<EarningsBean> pageOfDealsByInvestFee(int currPage,
			int pageSize, DealOption dealOption, DealType dealType, Date time,
			int exports) {
		return dealPlatformDao.pageOfDealsByInvestFee(currPage, pageSize, dealOption, dealType, time, exports);
		
	}	
}
